﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Collections;

namespace KursovickCS
{
    class Application
    {
        private static IObject ReadObject()
        {
            Console.WriteLine("Enter object type:");
            string oType = Console.ReadLine();
            string oTypeLower = oType.ToLower();
            if (oTypeLower.Equals("string"))
            {
                Console.WriteLine("Enter string value:");
                string readStr = Console.ReadLine();
                return new KursovickCS.String(readStr);
            }
            else if (oTypeLower.Equals("float"))
            {
                Console.WriteLine("Enter float value");
                string flValue = Console.ReadLine();
                float floatValue = float.Parse(flValue);
                return new KursovickCS.Float(floatValue);
            }
            else
            {
                Console.WriteLine("Unknown type");
                return null;
            }
        }
        static void Main(string[] args)
        {
            List<IObject> list = new List<IObject>();

            try
            {
                for (; ; )
                {
                    Console.WriteLine("Enter action: 0 to exit, 1 to add object, " +
                    "2 to delete object, 3 to save objects, 4 to restore objects, " +
                    "5 to view all object");

                    string line = Console.ReadLine();
                    if (line.Equals("0"))
                    {
                        return;
                    }
                    else if (line.Equals("1"))
                    {

                        IObject readObject = ReadObject();
                        if (null != readObject)
                        {
                            list.Add(readObject);
                        }
                    }
                    else if (line.Equals("3"))
                    {
                        Console.WriteLine("Enter file name to save objects:");
                        string fileName = Console.ReadLine();
                        FileStream stream = File.Create(fileName);
                        SoapFormatter formatter = new SoapFormatter();
                        int objectCount = list.Count();
                        formatter.Serialize(stream, objectCount);
                        foreach (IObject obj in list)
                        {
                            formatter.Serialize(stream, obj);
                        }

                        stream.Close();
                    }
                    else if (line.Equals("5"))
                    {
                        foreach (IObject obj in list)
                        {
                            Console.WriteLine("Object class: " + obj.Name() + ", value: " +
                                obj.UserData());
                        }
                    }
                    else if (line.Equals("4"))
                    {
                        Console.WriteLine("Enter file name:");
                        string fileName = Console.ReadLine();
                        FileStream stream = File.Open(fileName, FileMode.Open);
                        SoapFormatter formatter = new SoapFormatter();
                        int objectCount = (int)formatter.Deserialize(stream);
                        if (objectCount < 0)
                        {
                            Console.WriteLine("Error in object count");
                            continue;
                        }
                        list.Clear();
                        for (int i = 0; i < objectCount; i++)
                        {
                            list.Add((IObject)formatter.Deserialize(stream));
                        }
                        stream.Close();
                    }
                    else
                    {
                        Console.WriteLine("Unknown action");
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        internal static void Exit()
        {
            throw new NotImplementedException();
        }
    }
}
